package com.example.day06;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity3 extends AppCompatActivity {
    private static final String TAG = "MainActivity3";
    private View view1, view2, view3;
    private ViewPager viewPager;  //对应的viewPager
    private List<View> viewList;//view数组
    private FirstFragment firstFragment;
    private SecondFragment secondFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        firstFragment = new FirstFragment();
        secondFragment = new SecondFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        //这是主页面
        final DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
        //给按钮添加一个监听器
        findViewById(R.id.top_view_left_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 这里设置的方向要与layout里面的保持一致android:layout_gravity="end"
                //打开侧滑菜单
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                Log.w(TAG, "onDrawerSlide 侧边栏开始滑动");
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                Log.w(TAG, "onDrawerOpened  打开侧边栏");
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
                Log.w(TAG, "onDrawerClosed 关闭侧边栏");
            }


            @Override
            public void onDrawerStateChanged(int newState) {
                Log.w(TAG, "onDrawerStateChanged 侧边栏状态发生改变 newState=" + newState);
            }
        });
//主页面添加一个viewpager

        viewPager =(ViewPager) findViewById(R.id.viewpage3);
        view1 = getLayoutInflater().inflate(R.layout.main1,null);
        view2 = getLayoutInflater().inflate(R.layout.main2,null);
        view3 = getLayoutInflater().inflate(R.layout.main3,null);
        viewList = new ArrayList<>();
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        PagerAdapter pagerAdapter = new PagerAdapter() {
            /**
             * 判断是否page view与 instantiateItem(ViewGroup, int)返回的object的key 是否相同，
             * 以提供给其他的函数使用
             * @param arg0
             * @param arg1
             * @return
             */
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                //返回有效的View的个数
                return viewList.size();
            }

            /**
             * 该方法的功能是移除一个给定位置的页面。
             * @param container
             * @param position
             * @param object
             */
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }


            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }

        };
        viewPager.setAdapter(pagerAdapter);

//侧边栏
        String[] strings={"一","二","三","四"};
        String[] iconNames={"p1","p2","p3"};

        Map<String, List<String>> map = new HashMap();
        ArrayList<String> arr1 = new ArrayList<>();
        arr1.add("111111111111111111111111");
        arr1.add("222222222222222222");
        arr1.add("333333333333333333333");
        ArrayList<String> arr2 = new ArrayList<>();
        arr2.add("AAAAAAAAAAAAAAA");
        arr2.add("BBBBBBBBBBBBBBBBBBBBBBBBBB");
        arr2.add("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC");
        ArrayList<String> arr3 = new ArrayList<>();
        arr3.add("```````````````````````````");
        arr3.add("!!!!!!!!!!!!!!!!!!!!!!!!");
        arr3.add("小");
        ArrayList<String> arr4 = new ArrayList<>();
        arr4.add("```````````````````````````");
        arr4.add("!!!!!!!!!!!!!!!!!!!!!!!!");
        arr4.add("小");
        map.put(strings[0],arr1);
        map.put(strings[1],arr2);
        map.put(strings[2],arr3);
        map.put(strings[3],arr4);
        ExpandableListViewTest expandableListViewTest = new ExpandableListViewTest(strings,map,this,iconNames);
        ExpandableListView elv = findViewById(R.id.elv);
        elv.setAdapter(expandableListViewTest);


    }


}